﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Vit.Common;
using Vit.Common.Helpers;

namespace Vit.Web.Data
{
    public class EnumDataSourceProvider<TEnum> : IDataSourceProvider
    {
        #region IDataSourceProvider<TEnum> Members

        public string GetDisplayText(params object[] ids)
        {
            if (ids == null || ids.Length <= 0)
                return string.Empty;

            return Enum.ToObject(typeof(TEnum), ids[0]).ToString();
        }

        public object GetData(params object[] paras)
        {
            List<TEnum> vals = new List<TEnum>();
            foreach(string name in Enum.GetNames(typeof(TEnum)))
            {
                vals.Add(((TEnum)Enum.Parse(typeof(TEnum), name)));
            }

            return vals;
        }

        public IEnumerable<SelectListItem> GetListItems(params object[] paras)
        {
            List<SelectListItem> items = new List<SelectListItem>();
            var vals = EnumHelper.GetObjectsFromEnum(typeof(TEnum));
            foreach (var pair in vals)
            {
                items.Add(new SelectListItem()
                {
                    Text = pair.First,
                    Value = pair.Second.ToString()
                });
            }
            return items;
        }

        public Type EntityType
        {
            get { return typeof(TEnum); }
        }

        #endregion
    }
}
